<template>
  <div>
    <el-popover
      placement="bottom"
      width="346"
      trigger="click"
      @show="getUnreadMessage"
    >
      <div>
        <div class="layerTitle">
          <div class="layerTitle msgTitle">消息提醒</div>
          <div class="layerTitle msgCount">
            <div class="msgCountInner">{{ unreadCount }}</div>
          </div>
        </div>
        <el-table :data="msgData" height="250">
          <el-table-column>
            <template slot-scope="scope">
              <div class="msgContent">
                编号: {{ scope.row.businessNum }}
                <br />
                {{ scope.row.content }}
              </div>
              <div class="msgDate">{{ scope.row.createTime }}</div>
            </template>
          </el-table-column>
          <el-table-column width="50">
            <template slot-scope="scope">
              <el-link type="primary" @click="messageView(scope.row)"
                >查看</el-link
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="msgBottom">
          <el-link
            type="primary"
            class="msgReadAll"
            :disabled="allMarkReadDisabled"
            @click="msgReadAll"
            >全部已读</el-link
          >
        </div>
      </div>

      <el-badge slot="reference" :is-dot="unreadCount > 0" class="item">
        <i class="el-icon-bell" style="font-size: 24px"></i>
      </el-badge>
    </el-popover>
  </div>
</template>

<script>
import {
  markRead,
  getList,
  getUserUnreadMessageCount,
} from "../../api/Message/MessageHistory.js";

export default {
  name: "MessageIcon",
  data() {
    return {
      unreadCountInterval: null,
      unreadCount: 0, // 用户未读消息个数
      msgData: [],
      allMarkReadDisabled: true, // 全部已读按钮禁用状态
    };
  },
  watch: {
    unreadCount: {
      handler: function() {
        // 不可修改为()=>{},会导致this作用域异常
        this.getUnreadMessage();
        this.getNotifyMessage();
      },
    },
  },
  mounted() {
    this.getUnreadCount();
  },
  beforeDestroy() {
    this.destroy();
  },
  methods: {
    click() {},
    getUserUnreadMessageCountCommon() {
      getUserUnreadMessageCount().then((res) => {
        if (res.code === 200) {
          this.unreadCount = res.data;
          if (this.unreadCount > 0) {
            this.allMarkReadDisabled = false;
          } else {
            this.allMarkReadDisabled = true;
          }
        }
      });
    },
    getUnreadCount() {
      this.getUserUnreadMessageCountCommon();
      // 获取未读消息个数,5秒轮询一次
      this.unreadCountInterval = setInterval(() => {
        this.getUserUnreadMessageCountCommon();
      }, 5000);
    },
    getUnreadMessage() {
      // 获取未读消息
      this.msgData = [];
      var tmpPageSize = this.unreadCount <= 0 ? 99 : this.unreadCount;
      getList({ statusCode: 1 }, { pageIndex: 1, pageSize: tmpPageSize })
        .then((res) => {
          if (res.data.dataList) {
            this.msgData = res.data.dataList;
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
    msgReadAll() {
      // 全部已读
      this.allMarkReadDisabled = true;
      markRead({ statusCode: 3 })
        .then((res) => {
          this.allMarkReadDisabled = false;
          this.getUnreadCount();
        })
        .catch(() => {
          this.allMarkReadDisabled = false;
        });
    },
    getNotifyMessage() {
      // 获取右下角的通知消息,每次最多获取10个
      getList(
        { statusCode: 1, isNotification: true },
        { pageIndex: 1, pageSize: 10 }
      )
        .then((res) => {
          if (res.data.dataList) {
            res.data.dataList.forEach((element) => {
              setTimeout(() => {
                this.invokeNotify(element.businessNum, element.content);
              }, 500);
            });
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
    invokeNotify(num, content) {
      this.$notify({
        title: "消息提醒",
        position: "bottom-right",
        dangerouslyUseHTMLString: true,
        duration: 5000,
        message:
          `
            <div style="border-top:1px">
              <div>编号: ` +
          num +
          `</div>
              <div>` +
          content +
          `</div>
            </div>
          `,
      });
    },
    destroy() {
      if (this.unreadCountInterval) {
        clearInterval(this.unreadCountInterval);
      }
    },
    messageView(row) {
      // 字典表中配置参考
      // query 为具体业务的查询字段.同时需要在对应业务页面的created hook中赋值 this.queryForm.num = this.$route.query.num;
      // {"path":"/AAA/BBB","query":"num"}
      var tmpMessageTypeTarget = JSON.parse(row.messageTypeTarget);
      var tmpQuery = {};
      tmpQuery[tmpMessageTypeTarget.query] = row.businessNum;
      this.$router.push({
        path: tmpMessageTypeTarget.path,
        query: tmpQuery,
      });
    },
  },
};
</script>

<style scoped>
.item {
  margin-right: 5px;
}
.layerTitle {
  display: inline;
  float: left;
}
.layerTitle .msgTitle {
  font-size: 14px;
  font-family: MicrosoftYaHei;
  color: #232638;
  line-height: 22px;
}
.layerTitle .msgCount {
  height: 14px;
  background: #4787ff;
  border-radius: 9px;
  text-align: center;
  margin-top: 3.5px;
  margin-left: 5px;
}
.msgCountInner {
  margin-left: 5px;
  margin-right: 5px;
  font-size: 10px;
  font-family: ArialMT;
  color: #ffffff;
  line-height: 14px;
}

.msgContent {
  font-size: 14px;
  font-family: MicrosoftYaHei;
  color: #666666;
  line-height: 19px;
}
.msgDate {
  font-size: 14px;
  font-family: ArialMT;
  color: #a7a7a7;
  line-height: 16px;
}
.msgBottom {
  width: 100%;
  height: 38px;
  background: #f5f5f5;
  float: right;
}
.msgReadAll {
  float: right;
  margin-top: 10px;
  margin-right: 20px;
}
</style>
